﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using Shumisen.Core.Composition;

namespace Shumisen.Web.Form.Handlers
{
	public class PageHandlerFactory : System.Web.UI.PageHandlerFactory
	{
		public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
		{
			var handler = base.GetHandler(context, requestType, virtualPath, path);
			if (handler == null)
			{
				return null;
			}
			var container = ContainerHolder.Container;
			var part = AttributedModelServices.CreatePart(handler);
			if (!part.ImportDefinitions.Any())
			{
				return handler;
			}
			if (part.ExportDefinitions.Any())
			{
				// TODO 適切な例外
				throw new InvalidOperationException("Handlers cannot be exportable");
			}
			container.SatisfyImportsOnce(part);
			return handler;
		}
	}
}
